package org.itboys.core.service.mysql.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.dao.product.SundaySkuPriceMapper;
import org.itboys.core.entity.mysql.SundaySkuPrice;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/8/15
 * 描述：
 */
@Service
public class SundaySkuPriceService extends BaseMySqlService<SundaySkuPrice>{
    @Autowired
    private SundaySkuPriceMapper sundaySkuPriceMapper;

    public SundaySkuPrice getPrice	(Long skuId,BigDecimal skuInPrice,BigDecimal skuPrice){
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("skuId", skuId);
        SundaySkuPrice price = null;
        List<SundaySkuPrice> prices = super.select(param);
        if(ListUtil.isNotNull(prices)){
            price=prices.get(0);
        }else{
            price = new SundaySkuPrice(skuId,skuInPrice,skuPrice);
            super.insert(price);
        }
        return price;
    }

    /**
     * 查询
     * @param request
     * @return
     */
    public PageResult<SundaySkuPrice> selectPrice(HttpServletRequest request, Long skuId) {
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request);
        param.put("skuId",skuId);
        return new PageResult<SundaySkuPrice>(super.select(param), super.count(param));
    }

    /**
     * 批量更新价钱
     * @param skuPrices
     */
    public void update(List<SundaySkuPrice> skuPrices){
        sundaySkuPriceMapper.update(skuPrices);
    }
}
